#
# Usage:
# perl cleanup.pl [DIR]
# DIR: the directory to look for Space 404s, current directory by default
#

sub scanloc {
	my ($dir, $title) = @_;
	opendir($DH, $dir) || die "cannot open $dir $!\n";

	my $line;
	my @deathrow = ();

	foreach $entry (sort(readdir($DH))) {
		if($entry eq "." || $entry eq "..") {
			next;
		}

		$entry = $dir.$entry;

		open($FH, "$entry") || warn "cannnot open $entry\n";

		while($line = <$FH>) {
			if($line=~/Sorry, Spaces is temporarily unavailable at this time/) {
				print "$entry is a 404 - deleting...";
				push(@deathrow, $entry);
				print "done\n";
			}
		}
		close ($FH);
	}

	closedir($DH);

	unlink(@deathrow);
}

my $dir = $ARGV[0] || ".";

scanloc($dir);